import {Component, OnInit} from '@angular/core';
import {ActivatedRoute} from "@angular/router";
import {EmergencyButton} from "../../dto/EmergencyButton";
import {MatFormField, MatFormFieldModule, MatLabel} from "@angular/material/form-field";
import {MatInput, MatInputModule} from "@angular/material/input";
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators} from "@angular/forms";
import {MatIcon} from "@angular/material/icon";
import {MatOption} from "@angular/material/autocomplete";
import {MatSelect} from "@angular/material/select";
import {NgIf} from "@angular/common";
import {MatButton} from "@angular/material/button";
import {EmergencyFindService} from "../../service/emergency-find.service";
import {dummyLawyers} from "../../stubs/lawyer-stub";
import {
  MatCard,
  MatCardContent,
  MatCardHeader,
  MatCardSubtitle,
  MatCardTitleGroup,
  MatCardTitle,
  MatCardLgImage, MatCardImage, MatCardActions
} from "@angular/material/card";
import {MatRadioButton} from "@angular/material/radio";
import {DaDataService} from "../../service/da-data.service";
import {NgxMaskDirective} from "ngx-mask";


@Component({
  selector: 'app-emergency-cabinet',
  standalone: true,
  imports: [
    MatFormField,
    MatInput,
    MatLabel,
    FormsModule, MatFormFieldModule, MatInputModule, MatIcon,
    MatOption, MatSelect, NgIf, MatButton, MatCardHeader,
    MatCardTitleGroup, MatCardContent, MatCardSubtitle, MatCard, MatButton, MatCardTitle, MatCardLgImage, MatRadioButton, NgxMaskDirective, MatCardImage, MatCardActions, ReactiveFormsModule
  ],
  templateUrl: './emergency-cabinet.component.html',
  styleUrl: './emergency-cabinet.component.scss',

})
export class EmergencyCabinetComponent implements OnInit{
  clientData  = new FormGroup({
    firstName: new FormControl('', Validators.required),
    familyName: new FormControl('', Validators.required),
    middleName: new FormControl(''),
    phoneNumber: new FormControl('', Validators.required),
    address: new FormControl('', Validators.required),
    description: new FormControl('', Validators.required),
  })
  callType?: string
  callTypeValue?: string
  typeOfSpecialist?: string
  specialist: any
  firstName = ''
  middleName= ''
  address   = ''
  geoData = {
    geo_lat: '',
    geo_lon: '',
    qc_geo: '',
  }
  isNotGeoData = false

  viewLawyer = false

  constructor(private  activatedRoute: ActivatedRoute,
              private emergencyFindService: EmergencyFindService,
              private daDataService: DaDataService,
              // protected daData: DaData
  ) {
  }
  dummyLawyers  =  dummyLawyers

  ngOnInit(): void {
    this.callType = this.activatedRoute.snapshot.queryParams['callType']
    this.typeOfSpecialist  = this.activatedRoute.snapshot.queryParams['assistType']
    // @ts-ignore
    console.log(EmergencyButton[this.callType])
    // @ts-ignore
    this.callTypeValue = EmergencyButton[this.callType]

  }

  findAddress(name: string, value: string | string[]) {

  }

  findLawyer() {
      let client = {
        firstName: this.clientData.get(['firstName'])?.value,
        familyName: this.clientData.get(['familyName'])?.value,
        middleName: this.clientData.get(['middleName'])?.value,
        phoneNumber: this.clientData.get(['phoneNumber'])?.value,
        address: this.clientData.get(['address'])?.value,
        description: this.clientData.get(['description'])?.value,
        callType: this.callType,
        typeOfSpecialist: this.typeOfSpecialist,
        geoLat: this.geoData.geo_lat,
        geoLon: this.geoData.geo_lon,
      }
      console.log(client)
      this.emergencyFindService.getLawyer(client)
      this.getDummySpecialist()
      this.viewLawyer = !this.viewLawyer
  }

  getDummySpecialist()  {
    console.log('Заглушка отображения специалиста' + this.typeOfSpecialist + '  ' + this.callType)
    // @ts-ignore
    this.dummyLawyers.forEach( e  => {
      if (e.callType === this.callType) {
        this.specialist  =  e
    }})
    console.log(this.specialist)
  }

  getClientAddress($event: KeyboardEvent) {
    this.isNotGeoData = false
    // @ts-ignore
    this.daDataService.getAddressData($event.target.value)
  }

  getResult(): any {
    let result = this.daDataService.getResult()
    console.log(result)
    this.parseAddress(result)
  }

  parseAddress(address: string) {
    let addressGeo = JSON.parse(address)
    this.address = addressGeo.address
    this.geoData.geo_lat = addressGeo.geo_lat
    this.geoData.geo_lon = addressGeo.geo_lon
    this.geoData.qc_geo = addressGeo.qc_geo
    if (addressGeo.qc_geo > 1) {
      console.log(addressGeo.qc_geo !== 0)
      console.log(addressGeo.qc_geo !== 1)
      console.log('Точность гео данных - ', addressGeo.qc_geo)
      this.isNotGeoData = true
    }
    console.log(addressGeo.address)
  }
}
